package com.hjm.project.team.policy.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.team.policy.domain.TeamPolicy;
import com.hjm.project.team.policy.service.ITeamPolicyService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 团队政策信息Controller
 * 
 * @author maker-platform
 * @date 2021-04-11
 */
@Controller
@RequestMapping("/team/policy")
public class TeamPolicyController extends BaseController
{
    private String prefix = "team/policy";

    @Resource
    private ITeamPolicyService teamPolicyService;


    /**
     * 引流奖励
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("team:policy:view")
    @GetMapping()
    public String policy(ModelMap mmap) {
        return prefix + "/policy";
    }


    /**
     * 引流奖励
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("team:policy:view")
    @GetMapping("/policyCg")
    public String policyCg(ModelMap mmap) {
        return prefix + "/policyCg";
    }

    /**
     * 增量奖励
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("team:policy:view")
    @GetMapping("/policyZl")
    public String policyZl(ModelMap mmap) {
        return prefix + "/policyZl";
    }


    @RequiresPermissions("team:policy:add")
    @GetMapping("/addPolicy")
    public String addPolicy(ModelMap mmap) {
        TeamPolicy teamPolicy = new TeamPolicy();
        teamPolicy.setType("1");
        List<TeamPolicy> teamPolicies = teamPolicyService.selectTeamPolicyList(teamPolicy);  //获取返现编号 一种分润类型只有一种编号
        if (CollectionUtils.isEmpty(teamPolicies)) {
            mmap.put("policyId",teamPolicyService.getPolicyCode());
        } else {
            mmap.put("policyId",teamPolicies.get(0).getPolicyId());
        }
        return prefix + "/addPolicy";
    }





    /**
     * 采购奖励
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("team:policy:add")
    @GetMapping("/addPolicyCg")
    public String addPolicyCg(ModelMap mmap) {
        TeamPolicy teamPolicy = new TeamPolicy();
        teamPolicy.setType("2");
        List<TeamPolicy> teamPolicies = teamPolicyService.selectTeamPolicyList(teamPolicy);  //获取返现编号 一种分润类型只有一种编号
        if (CollectionUtils.isEmpty(teamPolicies)) {
            mmap.put("policyId",teamPolicyService.getPolicyCode());
        } else {
            mmap.put("policyId",teamPolicies.get(0).getPolicyId());
        }
        return prefix + "/addPolicyCg";
    }




    /**
     * 增量奖励添加页面
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("team:policy:view")
    @GetMapping("/addPolicyZl")
    public String addPolicyZl(ModelMap mmap) {
        TeamPolicy teamPolicy = new TeamPolicy();
        teamPolicy.setType("3");
        List<TeamPolicy> teamPolicies = teamPolicyService.selectTeamPolicyList(teamPolicy);  //获取返现编号 一种分润类型只有一种编号
        if (CollectionUtils.isEmpty(teamPolicies)) {
            mmap.put("policyId",teamPolicyService.getPolicyCode());
        } else {
            mmap.put("policyId",teamPolicies.get(0).getPolicyId());
        }
        return prefix + "/addPolicyZl";
    }



    /**
     * 查询团队政策信息列表
     */
    @RequiresPermissions("team:policy:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeamPolicy teamPolicy)
    {
        startPage();
        List<TeamPolicy> list = teamPolicyService.selectTeamPolicyList(teamPolicy);
        return getDataTable(list);
    }

    /**
     * 导出团队政策信息列表
     */
    @RequiresPermissions("team:policy:export")
    @Log(title = "团队政策信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeamPolicy teamPolicy)
    {
        List<TeamPolicy> list = teamPolicyService.selectTeamPolicyList(teamPolicy);
        ExcelUtil<TeamPolicy> util = new ExcelUtil<TeamPolicy>(TeamPolicy.class);
        return util.exportExcel(list, "policy");
    }

    /**
     * 新增团队政策信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存团队政策信息
     */
    @RequiresPermissions("team:policy:add")
    @Log(title = "团队政策信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeamPolicy teamPolicy)
    {
        return toAjax(teamPolicyService.insertTeamPolicy(teamPolicy));
    }

    /**
     * 修改引流政策信息
     */
    @GetMapping("/editPolicy")
    public String editPolicy(@RequestParam Map<String,String> params, ModelMap mmap)
    {
        TeamPolicy teamPolicy = teamPolicyService.selectTeamPolicyById(Integer.parseInt(params.get("id")));
        mmap.put("teamPolicy", teamPolicy);
        return prefix + "/editPolicy";
    }

    /**
     * 修改采购政策信息
     */
    @GetMapping("/editPolicyCg")
    public String editPolicyCg(@RequestParam Map<String,String> params, ModelMap mmap)
    {
        TeamPolicy teamPolicy = teamPolicyService.selectTeamPolicyById(Integer.parseInt(params.get("id")));
        mmap.put("teamPolicy", teamPolicy);
        return prefix + "/editPolicyCg";
    }

    /**
     * 修改采购政策信息
     */
    @GetMapping("/editPolicyZl")
    public String editPolicyZl(@RequestParam Map<String,String> params, ModelMap mmap)
    {
        TeamPolicy teamPolicy = teamPolicyService.selectTeamPolicyById(Integer.parseInt(params.get("id")));
        mmap.put("teamPolicy", teamPolicy);
        return prefix + "/editPolicyZl";
    }

    /**
     * 修改保存团队政策信息
     */
    @RequiresPermissions("team:policy:edit")
    @Log(title = "团队政策信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeamPolicy teamPolicy)
    {
        return toAjax(teamPolicyService.updateTeamPolicy(teamPolicy));
    }

    /**
     * 删除团队政策信息
     */
    @RequiresPermissions("team:policy:remove")
    @Log(title = "团队政策信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(teamPolicyService.deleteTeamPolicyByIds(ids));
    }
}
